using MicroMsg.Common.Utils;
using System;
namespace MicroMsg.Network
{
	public class NetPack
	{
		public class Header
		{
			public int totalLen;
			public short headLen;
			public short version;
			public int opCmd;
			public int int_0;
			public Header(int int_1, int dataLen, int opCmd)
			{
				this.headLen = 16;
				this.version = 1;
				this.totalLen = dataLen + (int)this.headLen;
				this.opCmd = opCmd;
				this.int_0 = int_1;
			}
		}
		public class Request
		{
			public NetPack.Header head;
			public byte[] body;
			public Request(int int_0, int int_1, byte[] data)
			{
				this.head = new NetPack.Header(int_0, data.Length, int_1);
				this.body = data;
			}
			public int getLength()
			{
				return this.head.totalLen;
			}
			public byte[] getBody()
			{
				return this.body;
			}
			public NetPack.Header getHead()
			{
				return this.head;
			}
			public int serializeToBuffer(ref byte[] outBuf)
			{
				outBuf = new byte[this.head.totalLen];
				int num = 0;
				Util.writeInt(this.head.totalLen, ref outBuf, ref num);
				Util.writeShort(this.head.headLen, ref outBuf, ref num);
				Util.writeShort(this.head.version, ref outBuf, ref num);
				Util.writeInt(this.head.opCmd, ref outBuf, ref num);
				Util.writeInt(this.head.int_0, ref outBuf, ref num);
				Util.writeBuffer(this.body, ref outBuf, ref num);
				return this.head.totalLen;
			}
		}
		public class Response
		{
			public NetPack.Header head;
			public byte[] body;
			public Response()
			{
				this.head = new NetPack.Header(0, 0, 0);
				this.body = new byte[0];
			}
			public Response(NetPack.Request request_0)
			{
				this.head = new NetPack.Header(request_0.head.int_0, 0, request_0.head.opCmd);
				this.body = new byte[0];
			}
			public int getLength()
			{
				return this.head.totalLen;
			}
			public byte[] getBody()
			{
				return this.body;
			}
			public NetPack.Header getHead()
			{
				return this.head;
			}
			public bool unserializeFromBuffer(byte[] buffer, int count)
			{
				int num = 0;
				this.head.totalLen = Util.readInt(buffer, ref num);
				this.head.headLen = Util.readShort(buffer, ref num);
				this.head.version = Util.readShort(buffer, ref num);
				if (this.head.headLen != 16 || this.head.version != 1)
				{
					Log.smethod_1("Network", string.Concat(new object[]
					{
						"unserialize invalid header, length=",
						this.head.headLen,
						", version=",
						this.head.version
					}));
				}
				if (this.head.totalLen > count)
				{
					Log.smethod_1("Network", string.Concat(new object[]
					{
						"unserialize invalid data-size, head.total=",
						this.head.headLen,
						", recv-data=",
						count
					}));
					return false;
				}
				this.head.opCmd = Util.readInt(buffer, ref num);
				this.head.int_0 = Util.readInt(buffer, ref num);
				int num2 = this.head.totalLen - (int)this.head.headLen;
				this.body = new byte[num2];
				Util.readBuffer(buffer, ref num, ref this.body, num2);
				return true;
			}
		}
	}
}
